package com.truyentranh.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

import com.truyentranh.R;
import com.truyentranh.controls.onUserCreateTopic;
import com.truyentranh.modules.local.MoreActivity;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

public class DisplayUtils {
	
	public static void openDialogCommentShareFacebook(final Activity context, final onUserCreateTopic onUserCreateTopic) {
		if (context != null) {
			final EditText topicName = new EditText(context);
			topicName.setPadding(2, 2, 2, 2);
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle("Nhập tin nhắn facebook");
			alert.setMessage("Tin nhắn: ");
			alert.setView(topicName);

			alert.setPositiveButton("Gửi", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					String strPinCode = topicName.getText().toString();
					if(onUserCreateTopic != null){
						onUserCreateTopic.onCreate(strPinCode);
					}
				}
			});
			
			alert.setNeutralButton("Ngẫu nhiên", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					if(onUserCreateTopic != null){
						onUserCreateTopic.onCreate("$RANDOM$");
					}
				}
			});

			alert.setNegativeButton("Hủy bỏ", null);
			alert.show();
		}
	}

	public static void openDialogConfirmFacebook(ViewGroup viewgroup,final Activity context, DialogInterface.OnClickListener yes) {
		if (context != null) {
			
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowHeader = inflater.inflate(R.layout.confirm_login_facebook, viewgroup, false);
			CheckBox dontask = (CheckBox) rowHeader.findViewById(R.id.dont_ask_cofirm_login_facebook);		
			alert.setView(rowHeader);

			dontask.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {					
					AppSettingParams.setIsDontAskIfNotLoginFB(context,isChecked);					
				}
			});
			alert.setPositiveButton("Xem bình luận", yes);
			alert.setNeutralButton("�?ăng nhập", new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(context, MoreActivity.class);
					context.startActivity(intent);					
				}
			});
			alert.setNegativeButton("Hủy bỏ",null);
			alert.show();
		}
	}

	public static void openDialogEnterNameTopic(Context context, final onUserCreateTopic onUserCreateTopic) {
		if (context != null) {
			final EditText topicName = new EditText(context);
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle("Tạo chủ đ�?");
			alert.setMessage("Nhập tên chủ đ�?:");
			alert.setView(topicName);

			alert.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					String strPinCode = topicName.getText().toString();
					if (onUserCreateTopic != null)
						onUserCreateTopic.onCreate(strPinCode);
				}
			});

			alert.setNegativeButton("Hủy bỏ", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					if (onUserCreateTopic != null)
						onUserCreateTopic.onCancel();
				}
			});
			alert.show();
		}
	}
	
	public static void openTipDialog(String title, 
			String contentMessage,
			final String detailLink,
			final Activity context) {
		if (context != null) {
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(title);
			alert.setMessage(contentMessage);
			alert.setPositiveButton("Đóng", null);
			if(detailLink != null && !"".equals(detailLink)){
				alert.setNeutralButton("Chi tiết", new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(detailLink));
						context.startActivity(browserIntent);
					}
				});
			}
			alert.show();
		}
	}

	public static void openNoticeDialog(String title, String contentMessage, Context context, DialogInterface.OnClickListener close) {
		if (context != null) {
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(title);
			alert.setMessage(contentMessage);
			alert.setPositiveButton("Đóng", close);
			alert.show();
		}
	}

	public static AlertDialog.Builder showUpdatesDialog(boolean force, String title, String contentMessage, Context context, DialogInterface.OnClickListener ok, DialogInterface.OnClickListener cancel, DialogInterface.OnClickListener detail) {
		if (context != null) {
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(title);
			alert.setMessage(contentMessage);
			alert.setPositiveButton("Cập nhật", ok);

			final String cancelText = force ? "Thoát" : "Hủy bỏ";
			alert.setNegativeButton(cancelText, cancel);

			return alert;
		}
		return null;
	}

	public static void openConfirmDialog(String title, String contentMessage, Context context, DialogInterface.OnClickListener ok, DialogInterface.OnClickListener cancel) {
		if (context != null) {
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(title);
			alert.setMessage(contentMessage);
			alert.setPositiveButton("Đồng ý", ok);
			alert.setNegativeButton("Hủy bỏ", cancel);
			alert.show();
		}
	}

	public static void openDialogEnterNameTopic(String title, String contentMessage, Context context, final onUserCreateTopic onUserCreateTopic) {
		if (context != null) {
			final EditText topicName = new EditText(context);
			AlertDialog.Builder alert = new AlertDialog.Builder(context);
			alert.setTitle(title);
			alert.setMessage(contentMessage);
			alert.setView(topicName);

			alert.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					String strPinCode = topicName.getText().toString();
					if (onUserCreateTopic != null)
						onUserCreateTopic.onCreate(strPinCode);
				}
			});

			alert.setNegativeButton("Hủy bỏ", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					if (onUserCreateTopic != null)
						onUserCreateTopic.onCancel();
				}
			});
			alert.show();
		}
	}
	
	public static void showMess(final String mess,final Activity currentActivity){
		try
		{
			
				if(mess != null && mess.equals("")) return;

				if(currentActivity != null && !currentActivity.isFinishing())
				{
					
					currentActivity.runOnUiThread(new Runnable() {					
						public void run() {

							try {
								if(currentActivity != null)
								{
									LayoutInflater inflater = currentActivity.getLayoutInflater();
									View layout = inflater.inflate(R.layout.layout_toast, (ViewGroup) currentActivity.findViewById(R.id.toast_layout_root));
									
									TextView text = (TextView) layout.findViewById(R.id.text);
									text.setText(mess);

									Toast toast = new Toast(MainApplication.getAppContext());
									toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);
									toast.setDuration(Toast.LENGTH_LONG);
									toast.setView(layout);
									toast.show();
								}
								else
								{
									Toast toast = Toast.makeText(MainApplication.getAppContext(), mess, Toast.LENGTH_SHORT);
									toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_HORIZONTAL, 0, 0);
									toast.show();
								}
							} catch (Exception e) {
								e.printStackTrace();
							}											
						}
					});
				}
				
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}				
	}
}
